package monitor

import (
	"hxy/repo/database"
	"hxy/repo/model"
	"log"
)

// TODO 默认在数据库里的所有git repository都要加入定时任务
func Init_monotir() {
	repositories, re := database.GetAllGitRepo()
	if re.Error != nil {
		panic("can't init corn in database")
	}
	for _, repository := range repositories {
		go add_git_monitor(repository)
	}
	log.Println("-----Init the Git Monitor Mod----")
}
func add_git_monitor(repository model.GitModel) {
	auth, re := database.AntuById(repository.AuthID)
	if re.Error != nil {
		log.Printf("Error in add git monitor :%s \n", re.Error)
		return
	}
	if re.RowsAffected == 0 {
		auth = model.GitAuth{}
	}
	_gitmanager := GitManager{
		Repo:   repository,
		Auth:   auth,
		Status: model.Pending,
	}
	// 创建任务
	errs := _gitmanager.CreateCornMonitor()
	if errs != nil {
		log.Printf("Error in add git monitor :%s \n", errs)
		return
	}
	poolmu.Lock()
	managerPool[_gitmanager.uid] = &_gitmanager
	poolmu.Unlock()
}
